package cn.com.fig.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.com.fig.dao.DictionaryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.mysql.jdbc.StringUtils;

@Service
public class DictionaryService {
	
	@Autowired
	private DictionaryMapper dao;
	
	public List<Map<String,Object>> listDic(String dicId){
		List<Map<String,Object>> list= null;
		
		Map<String,String> map = dao.getDicById(dicId);
		if(CollectionUtils.isEmpty(map)){
			return new ArrayList<Map<String,Object>>();
		}
		String tableName = map.get("tableName");
		String keyName = map.get("keyName");
		String valName = map.get("valName");
		String dicStatus = map.get("dicStatus");
		String searchWhere = map.get("searchWhere");
		
		if("0".equals(dicStatus)){
			return new ArrayList<Map<String,Object>>();
		}
		StringBuilder sb = new StringBuilder("SELECT ").append(keyName).append(",").append(valName).append(" FROM ").append(tableName);
		if(!StringUtils.isNullOrEmpty(searchWhere)){
			sb.append(" WHERE ").append(searchWhere);
		}
		
		list = dao.listDic(sb.toString());
		return list;
	}
}
